package com.crowntit.domain.activity.service.trail.node;

import com.crowntit.domain.activity.model.entity.MarketProductEntity;
import com.crowntit.domain.activity.model.entity.TrialBalanceEntity;
import com.crowntit.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import com.crowntit.domain.activity.service.trail.AbstractGroupBuyMarketSupport;
import com.crowntit.domain.activity.service.trail.factory.DefaultActivityStrategyFactory;
import com.crowntit.types.design.framework.tree.StrategyHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class TagNode extends AbstractGroupBuyMarketSupport<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> {

    @Resource
    private EndNode endNode;

    @Override
    protected TrialBalanceEntity doApply(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        GroupBuyActivityDiscountVO groupBuyActivityDiscountVO = dynamicContext.getGroupBuyActivityDiscountVO();
        String tagId = groupBuyActivityDiscountVO.getTagId();
        String userId = requestParameter.getUserId();

        if (activityRepository.isCrowdRange(userId, tagId)) {
            dynamicContext.setVisible(true);
            dynamicContext.setEnable(true);
            return router(requestParameter, dynamicContext);
        }

        dynamicContext.setVisible(groupBuyActivityDiscountVO.isVisible());
        dynamicContext.setEnable(groupBuyActivityDiscountVO.isEnable());
        return router(requestParameter, dynamicContext);
    }

    @Override
    public StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> get(MarketProductEntity requestParam, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        return endNode;
    }
}
